package com.mcx.modules.proto;

import com.mcx.modules.common.IProto;
import com.mcx.modules.common.MMO_DataStream;

import java.io.*;
import java.util.*;

/**
 * 服务器返回角色信息
 */
public class RoleOperation_SelectRoleInfoReturnProto implements IProto
{
    private static MMO_DataStream dataStream = new MMO_DataStream();

    @Override
    public short getProtoCode() { return 10010; }

    public Boolean IsSuccess; //是否成功
    public Integer MsgCode; //消息码
    public Integer Id; //玩家编号
    public String NickName; //玩家昵称
    public Integer Status; //玩家状态
    public Integer Level; //等级
    public Integer Sex; //性别
    public Integer Money; //金钱
    public Integer Exp; //经验
    public Integer CurrSelectLineupIndex; //当前阵容上场的角色索引
    public Integer CurrLineupId; //当前选择阵容Id
    public Integer LastInWorldMapId; //最后进入的世界地图编号
    public String LastInWorldMapPos; //最后进入的世界地图坐标

    public byte[] ToArray() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputStream outputStream = new DataOutputStream(out)) {
            dataStream.outputStream = outputStream;
            dataStream.WriteShort(getProtoCode());
            dataStream.WriteBool(IsSuccess);
            if(IsSuccess) {
                dataStream.WriteInt(Id);
                dataStream.WriteUTF8String(NickName);
                dataStream.WriteInt(Status);
                dataStream.WriteInt(Level);
                dataStream.WriteInt(Sex);
                dataStream.WriteInt(Money);
                dataStream.WriteInt(Exp);
                dataStream.WriteInt(CurrSelectLineupIndex);
                dataStream.WriteInt(CurrLineupId);
                dataStream.WriteInt(LastInWorldMapId);
                dataStream.WriteUTF8String(LastInWorldMapPos);
            }
            else {
                dataStream.WriteInt(MsgCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();    }
    public static RoleOperation_SelectRoleInfoReturnProto GetProto(byte[] buffer) {
        RoleOperation_SelectRoleInfoReturnProto proto = new RoleOperation_SelectRoleInfoReturnProto();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            dataStream.inputStream = inputStream;
            proto.IsSuccess = dataStream.ReadBool();
            if(proto.IsSuccess) {
                proto.Id = dataStream.ReadInt();
                proto.NickName = dataStream.ReadUTF8String();
                proto.Status = dataStream.ReadInt();
                proto.Level = dataStream.ReadInt();
                proto.Sex = dataStream.ReadInt();
                proto.Money = dataStream.ReadInt();
                proto.Exp = dataStream.ReadInt();
                proto.CurrSelectLineupIndex = dataStream.ReadInt();
                proto.CurrLineupId = dataStream.ReadInt();
                proto.LastInWorldMapId = dataStream.ReadInt();
                proto.LastInWorldMapPos = dataStream.ReadUTF8String();
            }
            else {
                proto.MsgCode = dataStream.ReadInt();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return proto;
    }
}